package org.beetl.sql.core.engine;

import java.io.IOException;
import java.util.Map;

import org.beetl.core.Tag;
import org.beetl.support.CommonSupport;
import org.beetl.support.LogSupport;

/**
 * 实现mybatis trim. <trim prefix="WHERE" prefixOverrides="AND |OR"> </trim>
 * <trim prefix="SET" suffixOverrides=",">
 *
 * @author zhoupan
 */
public class TrimXTag extends Tag {

 public static final String SPACE = " ";
 public static final char SEPARATOR_CHAR = '|';
 public static final String SUFFIX_OVERRIDES = "suffixOverrides";
 public static final String PREFIX_OVERRIDES = "prefixOverrides";
 public static final String SUFFIX = "suffix";
 public static final String PREFIX = "prefix";
 private String prefix = "";
 private String[] prefixOverrides;
 private String[] suffixOverrides;
 private String suffix = "";

 @SuppressWarnings("unchecked")
 public void render() {
  try {
   Object[] args = this.args;
   if (args != null) {
    for (Object arg : args) {
     if (arg instanceof Map) {
      Map<String, Object> params = (Map<String, Object>) arg;
      if (params.containsKey(PREFIX)) {
       this.prefix = (String) params.get(PREFIX);
      }
      if (params.containsKey(SUFFIX)) {
       this.suffix = (String) params.get(SUFFIX);
      }
      if (params.containsKey(PREFIX_OVERRIDES)) {
       this.prefixOverrides = CommonSupport.split((String) params.get(PREFIX_OVERRIDES), SEPARATOR_CHAR);
      }
      if (params.containsKey(SUFFIX_OVERRIDES)) {
       this.suffixOverrides = CommonSupport.split((String) params.get(SUFFIX_OVERRIDES), SEPARATOR_CHAR);
      }
     }
    }
   }
   StringBuilder sb = new StringBuilder();
   String sql = getBodyContent().getBody();
   boolean isSqlBlank = this.isSqlBlank(sql);
   // prefix
   if (!isSqlBlank && CommonSupport.isNotBlank(this.prefix)) {
    appendSql(sb, this.prefix);
   }
   // prefixOverrides
   String trimSql = CommonSupport.trim(sql);
   if (this.prefixOverrides != null && this.prefixOverrides.length > 0) {
    for (String prefixOverride : this.prefixOverrides) {
     if (CommonSupport.startsWithIgnoreCase(trimSql, prefixOverride)) {
      trimSql = CommonSupport.substringAfter(trimSql, prefixOverride);
     }
    }
    this.appendSql(sb, trimSql);
   }
   // suffixOverrides
   if (this.suffixOverrides != null && this.suffixOverrides.length > 0) {
    for (String suffixOverride : this.suffixOverrides) {
     if (CommonSupport.endsWithIgnoreCase(trimSql, suffixOverride)) {
      trimSql = CommonSupport.substringBeforeLast(trimSql, suffixOverride);
     }
    }
    this.appendSql(sb, trimSql);
   }
   // suffix
   if (!isSqlBlank && CommonSupport.isNotBlank(this.suffix)) {
    appendSql(sb, this.suffix);
   }

   this.ctx.byteWriter.writeString(sb.toString());
  } catch (IOException ie) {
   ie.printStackTrace();
   LogSupport.error("{}", ie);
  }

 }

 /**
  * 
  * @param sb
  * @param sql
  */
 public void appendSql(StringBuilder sb, String sql) {
  if (CommonSupport.isNotBlank(sql)) {
   sb.append(SPACE).append(sql).append(SPACE);
  }
 }

 public boolean isSqlBlank(String sql) {
  if (CommonSupport.isBlank(sql)) {
   return true;
  }
  return CommonSupport.trim(sql).isEmpty();
 }
}
